package controllers

import (
	"github.com/astaxie/beego"
	"shuxianghui/models"
	"HTshuxianghui/htmodels"
	"strconv"
	"time"
	"strings"
	"github.com/astaxie/beego/validation"
)

type MessagesController struct {
	beego.Controller
}

func (c *MessagesController)Prepare() {
	level := c.GetSession("utype")
	if level == nil {
		Message(&c.Controller, `/login`, "登录信息错误", "", false)
	}
	c.Data["utype"] = level.(int)
}

//带了参数导致bootstrap table无法正常使用
func (c *MessagesController)HTMessagesList_html() {
	str_MesageStatus := c.Ctx.Input.URL()[strings.LastIndex(c.Ctx.Input.URL(), "/") + 1:]
	MesageStatus, _ := strconv.Atoi(str_MesageStatus)
	c.Data["MesageStatus"] = MesageStatus
	str1 := c.Ctx.Input.URL()[:strings.LastIndex(c.Ctx.Input.URL(), "/")]
	str_Type := str1[strings.LastIndex(str1, "/") + 1:]
	Type, _ := strconv.Atoi(str_Type)
	c.Data["Type"] = Type
	c.TplName = "messages_list.html"
	c.Render()
}

func (c *MessagesController)HTMessagesUserMessageList() {

}

func (c *MessagesController)HTMessagesList() {
	MesageStatus, _ := c.GetInt("MesageStatus")
	Type, _ := c.GetInt("Type")
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "MessagesTime", "SendId", "ReceiveId", "Type", "Addonfileds1"}
	Messages := []models.Messages{}
	hasType := true
	if c.GetSession("utype") != 1000 {
		hasType = false
	}
	htmodels.HTMessagesGetList(&Messages, fileds, MesageStatus, Type, offset, limit, search, "ReceiveId", orderby, c.GetSession("uid").(int), hasType)
	count, _ := htmodels.HTMessagesGetCount(search, "ReceiveId", MesageStatus, Type, c.GetSession("uid").(int))
	c.Data["json"] = map[string]interface{}{"rows":&Messages, "total":count}
	c.ServeJSON()
}

func (c *MessagesController)HTMessagesDetail() {
	Id, _ := c.GetInt64("Id")
	if Id != 0 {
		c.Data["update"] = true
		SetMessagesDetailInfo(c, Id)
	}
	mytype, _ := c.GetInt("type")
	c.Data["Type"] = mytype
	c.TplName = "messages_detail.html"
}

func SetMessagesDetailInfo(c *MessagesController, Id int64) {
	Messages := models.Messages{Id:int64(Id)}
	htmodels.HTMessagesGetDetail(&Messages)
	c.Data["Messages"] = &Messages
}

func (c *MessagesController)HTMessagesAdd() {
	Messages := models.Messages{}
	if err := c.ParseForm(&Messages); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("messagestime")); err != nil {
		Messages.MessagesTime = time.Now()
	} else {
		Messages.MessagesTime = time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&Messages); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTMessagesAdd(&Messages); err != nil {
		Message(&c.Controller, "", "添加失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/messages/detail`, "添加成功！", "", false)
}

func (c *MessagesController)HTMessagesUpdate() {
	Id, _ := c.GetInt64("Id");
	Messages := models.Messages{Id:int64(Id)}
	if err := htmodels.HTMessagesGetDetail(&Messages); err != nil {
		Message(&c.Controller, "", "严重错误，请联系管理员,即将返回！" + err.Error(), "", true)
	}
	if err := c.ParseForm(&Messages); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("messagestime")); err != nil {
		Messages.MessagesTime = time.Now()
	} else {
		Messages.MessagesTime = time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&Messages); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTMessagesUpdate(&Messages); err != nil {
		Message(&c.Controller, "", "更新失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/messages/list_html` + `/` + strconv.Itoa(Messages.Type) + `/` + strconv.Itoa(Messages.MesageStatus), "更新成功！", "", false)
}

func (c *MessagesController)HTMessagesDelete() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	count, err := htmodels.HTMessagesDelete(Ids)
	if err != nil {
		Json_Message(&c.Controller, 0, "删除失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "删除" + strconv.FormatInt(count, 10) + "行")
}

func (c *MessagesController)HTMessagesStatusChange() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	MesageStatus, err1 := c.GetInt("ChangedStatus")
	if err1 != nil {
		Json_Message(&c.Controller, 0, "更改失败")
	}
	count, err := htmodels.HTMessagesStatusChange(Ids, MesageStatus)
	if err != nil {
		Json_Message(&c.Controller, 0, "更改失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "更改" + strconv.FormatInt(count, 10) + "行状态")
}

func (c *MessagesController)HTMessagesvisit() {
	Id, _ := c.GetInt64("Id")
	if Id == 0 {
		Message(&c.Controller, "", "Id错误，即将返回！", "", true)
	}
	Messages := models.Messages{Id:Id}
	htmodels.HTMessagesGetDetail(&Messages)
	mytype:= Messages.Type
	if mytype>6{
		mytype=0
	}
	label := []string{"留言", "私信", "积分消息", "文章评论", "文章收藏", "订单消息", "审核消息"}
	if mytype > 6 {
		mytype = 0
	}
	c.Data["Type"] = label[mytype]
	c.Data["Messages"] = &Messages
	c.TplName = "messages_visit.html"
}

func (c *MessagesController)HTMessagesread() {
	Id, _ := c.GetInt64("Id")
	if Id == 0 {
		Json_Message(&c.Controller, 0, "Id错误")
	}
	MesageStatus:=1
	Ids:=[]int64{Id}
	if _,err:=htmodels.HTMessagesStatusChange(Ids, MesageStatus);err!=nil{
		Json_Message(&c.Controller, 0, "更改失败")
	}
	Json_Message(&c.Controller, 0, "已读成功")
}

func SendMessage(message *models.Messages) {
	htmodels.HTMessagesAdd(message)
}
